//
//  XCHPlay.m
//  视频悬浮框OC
//
//  Created by 解楚豪 on 2018/9/10.
//  Copyright © 2018年 xch. All rights reserved.
//

#import "XCHPlay.h"

static XCHPlay *instance;

@interface XCHPlay ()

@property (nonatomic, copy) NSString *currentUrl;

@end

@implementation XCHPlay

#pragma mark ---------------单利创建--------------------
+(instancetype)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    return [[self class] shareInstance];
}

-(instancetype)copyWithZone:(struct _NSZone *)zone{
    return [[self class]shareInstance];
}

#pragma mark --------------------init----------------------
-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

#pragma mark ------------------方法--------------------
-(void)addSupview:(UIView *)view{
    [view.layer addSublayer:self.playerLayer];
}

-(void)deleteVideoLayer{
    self.player = nil;
    self.playerLayer = nil;
    self.currentUrl = nil;
}

-(void)videoPlay{
    [self.player play];
}

-(void)videoPause{
    [self.player pause];
}

#pragma mark -------------------UI--------------------
-(void)defaultWithPlay{
    if (![self.currentUrl isEqualToString:self.videoURL]) {
        [self deleteVideoLayer];
        self.currentUrl = self.videoURL;
        NSURL *videoUrl = [NSURL URLWithString:self.videoURL];
        self.player = [AVPlayer playerWithURL:videoUrl];
        self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    }
}

#pragma mark --------------------Setter--------------
-(void)setVideoURL:(NSString *)videoURL{
    _videoURL = videoURL;
    //添加视频播放
    [self defaultWithPlay];
}

-(void)setIsSuspend:(BOOL)isSuspend{
    _isSuspend = isSuspend;
    
}

-(void)setIsFull:(BOOL)isFull{
    _isFull = isFull;
    
}

-(void)setVideoFrame:(CGRect)videoFrame{
    //视频播放范围
    self.playerLayer.frame = videoFrame;
}

-(void)setVideoGravity:(AVLayerVideoGravity)videoGravity{
    //视频展示模式
    self.playerLayer.videoGravity = videoGravity;
}

@end
